package com.ctgu.service.impl;

import com.ctgu.mapper.ScoreMapper;
import com.ctgu.service.ScoreService;
import com.ctgu.vo.Score;
import com.ctgu.vo.ScoreKey;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Resource
    private ScoreMapper scoreMapper;

    @Override
    public List<Score> selectScoreByStudentId(Integer studentId) {
        List<Score> scoreList = new ArrayList<>();
        scoreList = scoreMapper.selectScoreByStudentId(studentId);
        return scoreList;
    }

    @Override
    public Score selectBycourseIdstudentId(int studentId, String courseId) {
        ScoreKey scoreKey = new ScoreKey();
        scoreKey.setCourseId(courseId);
        scoreKey.setStudentId(studentId);
        return scoreMapper.selectByPrimaryKey(scoreKey);
    }

    @Override
    public void insertScore(int studentId, String courseId) {
        Score score = new Score();
        score.setCourseId(courseId);
        score.setStudentId(studentId);
        scoreMapper.insert(score);
    }

    @Override
    public int deleteCourse(Integer studentId, String courseId) {
        ScoreKey scoreKey = new ScoreKey();
        scoreKey.setStudentId(studentId);
        scoreKey.setCourseId(courseId);
        return scoreMapper.deleteByPrimaryKey(scoreKey);
    }

    @Override
    public List<Score> selectStudentInClass(String courseId) {
        return scoreMapper.selectStudentInClass(courseId);
    }

    @Override
    public void updateScoreByKey(int studentId, String courseId, int score) {
        Score scoreTemp = new Score();
        scoreTemp.setCourseId(courseId);
        scoreTemp.setStudentId(studentId);
        scoreTemp.setScore(score);
        scoreMapper.updateByPrimaryKeySelective(scoreTemp);
    }
}
